package cc.rengu.igas.channel.upac.core.service.trans;


import cc.rengu.igas.channel.upac.common.constant.UpacTreeNodeConstant;
import cc.rengu.igas.channel.upac.common.enums.RespCodeEnum;
import cc.rengu.igas.channel.upac.common.enums.UpacProdEnum;
import cc.rengu.igas.channel.upac.core.model.ChnlBizResponse;
import cc.rengu.igas.channel.upac.core.service.base.TokenPayService;
import cc.rengu.igas.channel.upac.facade.request.TokenConsumeRefundRequest;
import cc.rengu.igas.channel.upac.facade.response.TokenConsumeRefundResponse;
import cc.rengu.oltp.service.common.constant.TreeNodeConstant;
import cc.rengu.oltp.service.common.entity.DstChannelInfo;
import cc.rengu.oltp.service.common.enums.OltpRpcdEnum;
import cc.rengu.oltp.service.model.BizException;
import cc.rengu.oltp.utility.util.ConvertUtil;
import cc.rengu.oltp.utility.util.XmlTreeUtil;

/**
 * token消费退款服务 on 2019/10/8.
 */
public class TokenConsumeRefundService extends TokenPayService {

    @Override
    protected Object beforeBizProcess() throws Exception {
        TokenConsumeRefundRequest tokenConsumeRefundRequest = new TokenConsumeRefundRequest();
        ConvertUtil.convertOutput(tokenConsumeRefundRequest);
        return tokenConsumeRefundRequest;
    }

    @Override
    protected ChnlBizResponse callBizService(Object request) throws Exception {
        XmlTreeUtil xmlTreeUtil = new XmlTreeUtil();
        int iReturnCode = 0;
        ChnlBizResponse chnlBizResponse = new ChnlBizResponse();
        TokenConsumeRefundResponse tokenConsumeRefundResponse = new TokenConsumeRefundResponse();
        TokenConsumeRefundRequest tokenConsumeRefundRequest = (TokenConsumeRefundRequest) request;
        DstChannelInfo dstChannelInfo = (DstChannelInfo) xmlTreeUtil.getXmlTreeObjectValue(UpacTreeNodeConstant.DST_CHANNEL_INFO, DstChannelInfo.class);
        ConvertUtil.convertInput(tokenConsumeRefundRequest);
        xmlTreeUtil.setXmlTreeStringValue(UpacTreeNodeConstant.txnType, "04");
        xmlTreeUtil.setXmlTreeStringValue(UpacTreeNodeConstant.txnSubType, "00");
        xmlTreeUtil.setXmlTreeStringValue(UpacTreeNodeConstant.bizType, UpacProdEnum.TOKEN_PAY.getProdCode());
        xmlTreeUtil.setXmlTreeStringValue(TreeNodeConstant.TXN_NUM, tokenConsumeRefundRequest.getHeader().getTxnNum());
        iReturnCode = schedule.callService(dstChannelInfo.getDstServiceName(), 0);
        if (98 == iReturnCode) {
            rglog.error("调用银联全渠道服务<{}>超时，iReturnCode:<{}>", dstChannelInfo.getDstServiceName(), iReturnCode);
            tokenConsumeRefundResponse.setRespCode(OltpRpcdEnum.BIZ_PROCESS_TIMEOUT.getRespCode());
            tokenConsumeRefundResponse.setRespDesc(OltpRpcdEnum.BIZ_PROCESS_TIMEOUT.getRespDesc());
            chnlBizResponse.setResult(tokenConsumeRefundResponse);
            return chnlBizResponse;
        } else if (0 != iReturnCode) {
            rglog.error("调用银联全渠道服务<{}>失败，iReturnCode:<{}>", dstChannelInfo.getDstServiceName(), iReturnCode);
            throw new BizException(RespCodeEnum.CALL_BIZ_SERVICE_ERROR.getRespCode(), RespCodeEnum.CALL_BIZ_SERVICE_ERROR.getRespDesc());
        }
        ConvertUtil.convertOutput(tokenConsumeRefundResponse);
        tokenConsumeRefundResponse.setRespCode(convertRespCode(tokenConsumeRefundResponse.getRespCode()));

        chnlBizResponse.setResult(tokenConsumeRefundResponse);
        return chnlBizResponse;
    }

    @Override
    protected void afterBizProcess(ChnlBizResponse response) throws Exception {
        TokenConsumeRefundResponse tokenConsumeRefundResponse = (TokenConsumeRefundResponse) response.getResult();
        ConvertUtil.convertInput(tokenConsumeRefundResponse);
    }


}
